<?php

class LoginController extends Controller {

    public function actionIndex() {
        $model = new login_form;

        // if it is ajax validation request
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }

        // collect user input data
        if (isset($_POST['login_form'])) {
            $model->attributes = $_POST['login_form'];

            // validate user input and redirect to the previous page if valid
            if ($model->validate() && $model->login()) {
                //$this->redirect('/member/main/dashboard');
                Yii::app()->controller->redirect(array('/member/main/information'));
            }
        }
        // display the login form
        //$this->render('index', array('model' => $model));
        $this->render('index', array('model' => $model));
    }

    public function actionCekMember() {
        //$query = "SELECT * FROM sys_member WHERE network_mid='".$_POST['id_member']."'";
        $m = $_GET['m'];
        if (!empty($m)) {
            $sql = "SELECT m.member_network_id,m.member_name FROM sys_member m 
                LEFT JOIN sys_network n ON m.member_network_id=n.network_id WHERE n.network_mid='" .$m. "' AND n.network_level_status != 'passive'";
            $query = Yii::app()->db->createCommand($sql)->queryRow();
            if (!empty($query)) {
                echo $query['member_name'];
            } else {
                echo "Data tidak ada";
            }
        } else {
            echo" ";
        }
        //echo $_GET['m'];
    }

    public function actionReqActivation() {
        $model = new sys_member_activation_request;

        //$model->unsetAttributes();
//        die($_POST['id_member']);
        $query = "SELECT * FROM sys_network WHERE network_mid='" . $_POST['id_member'] . "'";
        $id = Yii::app()->db->createCommand($query)->queryRow();
        //echo $id['id_member'];
        if (isset($_POST['sys_member_activation_request'])) {

            //$model->attributes=$_POST['sys_member_activation_request'];
            //$CekID = sys_network::model()->find('network_mid=' . $_POST['id_member']);         
            //$IdMember = Yii::app()->db->createCommand($CekID)->queryRow();
            $model->member_activation_request_network_id = $id['network_id'];
            $model->member_activation_request_quantity = $_POST['sys_member_activation_request']['member_activation_request_quantity'];
            $model->member_activation_request_note = $_POST['sys_member_activation_request']['member_activation_request_note'];
//            $model->member_activation_request_status =$_POST['SysMemberActivationRequest']['member_activation_request_status'];
            $model->member_activation_request_datetime = $_POST['sys_member_activation_request']['member_activation_request_datetime'];


            //$model->member_activation_request_datetime ='';
//            if($model->validate()){
//                echo "woyoy";
//            } else {
//                echo "yuhuu";
//            }
//            
            //die();
//            echo"<pre>";
//            print_r($_POST['sys_member_activation_request']);
//            die();
//
//            die('aaaas');

            if ($model->save()) {

//                /die('berhasil');
                Yii::app()->user->setFlash('success', '<strong>Selamat !!</strong> Pengajuan perpanjangan masa aktif berhasil disimpan, dan akan segera kami proses. Terima Kasih');
                $this->redirect(array('ReqActivation'));
            } else {
                //die('gagal');
                Yii::app()->user->setFlash('success', '<strong>Terjadi Masalah !!</strong> Data tidak dapat disimpan.');
                $this->redirect(array('ReqActivation'));
            }

//            print_r($model->save());
//            die();
        }

        $this->render('create', array('model' => $model));
    }

}
